package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.entity.Server;
import cn.edu.sgu.www.mhxysy.pager.ServerPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.ServerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/server", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class ServerController {

	private final ServerService serverService;

	@Autowired
	public ServerController(ServerService serverService) {
		this.serverService = serverService;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(Server server) {
		serverService.insert(server);

		return JsonResult.success("添加成功");
	}

	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(@RequestParam Integer id) {
		serverService.deleteById(id);

		return JsonResult.success("删除成功");
	}

	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(Server server) {
		serverService.updateById(server);

		return JsonResult.success("修改成功");
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Server>> selectAll() {
		List<Server> list = serverService.selectAll();

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Server> selectById(@RequestParam Integer id) {
		Server server = serverService.selectById(id);

		return JsonResult.success(null, server);
	}

	@RequestMapping(value = "/selectByAreaId", method = RequestMethod.GET)
	public JsonResult<List<Server>> selectByAreaId(@RequestParam Integer areaId) {
		List<Server> list = serverService.selectByAreaId(areaId);

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Server>> selectByPage(ServerPager pager) {
		Page<Server> result = serverService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}